/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.orm.testdesign;

import java.util.HashMap;
import java.util.LinkedList;
import org.orm.PersistentException;
import org.orm.PersistentTransaction;
import org.testis.orm.classes.T_test_designtree;
import org.testis.orm.classes.T_test_stepcontainer;
import org.testis.orm.manager.TestisDbPersistentManager;
import org.testis.orm.refinedclasses.TestCaseGroupRefined;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeRefined;
import org.testis.orm.refinedclasses.TestDesignTreeRefined.TestDesignTreeParameters;
import org.testis.utils.middleware.TestDesign;

/**
 * 
 * @author nurhat
 */
public class TestCaseGroupDataHandler extends TestPartDataHandler {

    @Override
    public TestDesignTreeNode operationAdd(HashMap<String, Object> params,
	    TestDesignTreeNode parentNode, int childIndex) {
	TestDesignTreeNode newNode = null;
	String name = (String) params.get("name");
	String description = (String) params.get("description");
	String runAlways = (String) params.get("runAlways");
	String owner = (String) params.get("owner");
	String timeout = (String) params.get("timeout");
	LinkedList<String> labels = (LinkedList<String>) params.get("labels");
	T_test_stepcontainer container = null;
	try {
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    try {
		container = new T_test_stepcontainer();
		container.setName(name);
		container.setDescription(description);
		container.setContainerTypeId(TestDesign
			.getDesignPartTypeIdByName("TestCaseGroup"));
		container.save();
		TestDesign.saveLabels(labels, container);
		T_test_designtree obj = new T_test_designtree();
		obj.save();
		TestDesignTreeRefined designTree = TestDesignTreeRefined
			.getDesignTree(obj);
		designTree.getTreeReference().setTypeId(
			container.getContainerTypeId());
		designTree.getTreeReference().setParent(
			parentNode.getReference().getTreeReference());
		designTree.getTreeReference().setElementId(container.getId());
		designTree.getTreeReference().save();

		if (owner != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(),
			    TestDesign.m_owners.get(owner).getId());
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.OwnerId.name(), 0);
		}
		designTree.setParameterValue(
			TestDesignTreeParameters.SuccessPercentage.name(), 100);
		if (runAlways != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.RunAlways.name(),
			    Boolean.parseBoolean(runAlways));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.RunAlways.name(), false);
		}
		if (timeout != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.Timeout.name(),
			    Integer.parseInt(timeout));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.Timeout.name(), null);
		}
		designTree.getTreeReference().save();
		int place = TestDesign.setRankOfAdd(parentNode,
			designTree.getTreeReference(), childIndex);
		designTree.getTreeReference().save();

		t.commit();
		TestCaseGroupRefined result = new TestCaseGroupRefined(
			container);
		newNode = TestDesignTreeNode.getNode(designTree, result);
		if (place == -1) {
		    parentNode.addChild(newNode);
		} else {
		    parentNode.addChild(place, newNode);
		    newNode.setPlace(place);
		}
	    } catch (PersistentException ex) {
		t.rollback();
		ex.printStackTrace();
		newNode = null;
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	    newNode = null;
	}
	return newNode;
    }

    @Override
    public boolean operationSave(HashMap<String, Object> params,
	    TestDesignTreeNode node) {
	String name = (String) params.get("name");
	String description = (String) params.get("description");
	String runAlways = (String) params.get("runAlways");
	LinkedList<String> labels = (LinkedList<String>) params.get("labels");

	TestDesignTreeRefined designTree = node.getReference();
	TestCaseGroupRefined container = (TestCaseGroupRefined) node
		.getObject();
	try {
	    PersistentTransaction t = TestisDbPersistentManager.instance()
		    .getSession().beginTransaction();
	    try {
		container.getReference().setName(name);
		container.getReference().setDescription(description);
		container.getReference().save();
		if (runAlways != null) {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.RunAlways.name(),
			    Boolean.parseBoolean(runAlways));
		} else {
		    designTree.setParameterValue(
			    TestDesignTreeParameters.RunAlways.name(), false);
		}
		designTree.getTreeReference().save();
		TestDesign.saveLabels(labels, container.getReference());
		t.commit();
		return true;
	    } catch (PersistentException ex) {
		t.rollback();
		ex.printStackTrace();
	    } finally {
		TestisDbPersistentManager.instance().disposePersistentManager();
	    }
	} catch (PersistentException ex) {
	    ex.printStackTrace();
	}
	return false;

    }
}
